<?php

namespace App\Models;


class Action extends BaseModel
{
    protected $table = "action";

    /* 模型自动验证 */
    protected $rules = [
        'name' => 'required|regex:/^[a-zA-Z]\w{0,39}$/|unique:action',
        'title' => 'required|between:1,80',
        'remark' => 'required|between:1,140',
    ];

    protected $message = [
        'name.required' => '行为标识必须',
        'name.regex' => '标识不合法',
        'name.unique' => '标识已经存在',
        'title.required' => '标题不能为空',
        'title.between' => '标题长度不能超过80个字符',
        'remark.required' => '行为描述不能为空',
        'remark.between' => '行为描述不能超过140个字符',
    ];

    public function __construct()
    {
        parent::__construct();
    }

    public function addOrUpdate()
    {
        $data['id'] = trim(request('id'));
        $data['name'] = trim(request('name'));
        $data['title'] = trim(request('title'));
        $data['remark'] = trim(request('remark'));
        $this->rules['name'] =  $this->rules['name'] . ',id,' . $data['id'];
        if(!$this->_validate($data)){
            return false;
        }
        $data['rule'] = trim(request('rule'));
        $data['log'] = trim(request('log'));
        $data['type'] = trim(request('type'));
        $data['status'] = trim(request('status'));
        return $this->_save($data);
    }


    public function getByName($name)
    {
        return $this->_getOne(['name'=>$name]);
    }
}